`
https://leetcode.cn/problems/trapping-rain-water/
`

/**
 * @param {number[]} height
 * @return {number}
 */
var trap = function (height) {
  if (height.length === 0) return 0
  const n = height.length
  const l_max = new Array(n).fill(0)
  const r_max = new Array(n).fill(0)
  // 初始化 base case
  l_max[0] = height[0]
  r_max[n - 1] = height[n - 1]
  // 从左向右计算 l_max
  for (var i = 1; i < n; i++) {
    l_max[i] = Math.max(height[i], l_max[i - 1]);
  }
  // 从右向左计算 r_max
  for (var i = n - 2; i >= 0; i--) {
    r_max[i] = Math.max(height[i], r_max[i + 1]);
  }
  let res = 0
  for (let i = 1; i < n - 1; i++) {
    res += Math.min(l_max[i], r_max[i]) - height[i]
  }
  return res
};